package org.itli.jnsocket.Mapper;

import org.apache.ibatis.annotations.*;
import org.itli.jnsocket.DTO.ProductDTO;
import org.itli.jnsocket.Pojo.Goods;
import org.itli.jnsocket.Pojo.OnBound;
import org.itli.jnsocket.Pojo.Outbound;
import org.itli.jnsocket.Pojo.Supplier;

import java.math.BigInteger;

@Mapper
public interface OnBoundMapper {

    //-----------------------------------进货事务-----------------------------------------

    //向进货记录表写入
    @Insert("INSERT INTO jn_wms_purchase_order (" +
            "    order_sn," +
            "    supplier_id," +
            "    supplier_no," +
            "    supplier_name," +
            "    supplier_phone," +
            "    supplier_contact," +
            "    supplier_address," +
            "    purchase_amount," +
            "    purchase_type," +
            "    purchase_date," +
            "    pay_type," +
            "    remark," +
            "    create_time," +
            "    update_time," +
            "    create_by," +
            "    update_by," +
            "    del_flag," +
            "    warehouse_id " +
            ") VALUES ( " +
            "    #{b.order_sn}," +
            "    #{supplier_id}," +
            "    #{s.supplier_no}," +
            "    #{s.supplier_name}," +
            "    #{s.supplier_phone}," +
            "    #{s.supplier_contact}," +
            "    #{s.supplier_address}," +
            "    #{b.purchase_amount}," +
            "    #{b.purchase_type}," +
            "    #{b.purchase_date}," +
            "    #{b.pay_type}," +
            "    #{b.remark}," +
            "    NOW()," +
            "    NOW()," +
            "    #{create_by}," +
            "    #{update_by}," +
            "    '0'," +
            "    #{b.warehouse_id} " +
            ")")
    int Insert_Purchase_Order(OnBound b, String create_by, String update_by, Supplier s, BigInteger supplier_id);

    // 查询进货记录 id
    @Select("select id from jn_wms_purchase_order where order_sn = #{order_sn}")
    BigInteger Select_id(String order_sn);

    //根据 id 查询商品sn
    @Select("select goods_sn from jn_wms_goods where id = #{id}")
    String Select_Good_SN(BigInteger id);

    //根据 id 查询商品商品规格
    @Select("select goods_spec from jn_wms_goods where id = #{id}")
    String Select_Good_SPEC(BigInteger id);

    //向进货记录商品表插入
    @Insert("INSERT INTO jn_wms_purchase_order_goods (" +
            "    purchase_id," +
            "    order_sn," +
            "    goods_id," +
            "    goods_sn," +
            "    goods_name," +
            "    goods_spec," +
            "    goods_price_in," +
            "    goods_count," +
            "    create_time," +
            "    update_time," +
            "    create_by," +
            "    update_by," +
            "    del_flag " +
            ") VALUES (" +
            "    #{purchase_id}, " +
            "    #{order_sn}, " +
            "    #{p.goods_id}, " +
            "    #{p.goods_sn}, " +
            "    #{p.goods_name}, " +
            "    #{p.goods_spec}, " +
            "    #{p.goods_price_in}, " +
            "    #{p.goods_count}, " +
            "    NOW(), " +
            "    NOW(), " +
            "    #{create_by}, " +
            "    #{update_by}, " +
            "    '0' " +
            ");")
    int Insert_purchase_order_goods(BigInteger purchase_id, String order_sn, ProductDTO p,String create_by,String update_by);


    //---------------情况一 查出来了 直接修改-------------

    //查询出该仓库的该商品剩余的
    @Select("select * from jn_wms_stock where goods_id = #{goods_id} AND warehouse_id = #{warehouse_id}")
    Goods Select_GoodsStock(BigInteger goods_id,BigInteger warehouse_id);

    //修改该仓库该商品剩余的数量
    @Update("update jn_wms_stock set goods_stock = #{goods_stock} where goods_id = #{goods_id} and warehouse_id = #{warehouse_id}")
    int UpdateGoods_Stock(BigInteger goods_id,BigInteger warehouse_id,int goods_stock);

    //-------------------------------------------------

    //--------------情况二 没查出来 直接插入--------------
    @Insert("INSERT INTO jn_wms_stock (" +
            "  goods_id," +
            "  goods_sn," +
            "  goods_stock," +
            "  remark," +
            "  create_by," +
            "  create_time," +
            "  update_by," +
            "  update_time," +
            "  del_flag," +
            "  warehouse_id " +
            ") VALUES ( " +
            "  #{goods_id}," +
            "  #{goods_sn}," +
            "  #{goods_stock}," +
            "  #{remark}," +
            "  #{create_by}," +
            "  NOW()," +
            "  #{update_by}," +
            "  NOW()," +
            "  '0'," +
            "  #{warehouse_id}" +
            ")")
    int Insert_Stock(BigInteger goods_id,BigInteger warehouse_id,int goods_stock,String goods_sn,String remark,String create_by,String update_by);



}
